@echo off
rem botchk.bat - by FireEgl@EFNet (FireEgl@LinuxFan.com) - 11/17/00

rem Description:
rem It's a botchk batch for "Windrops"..
rem Each time you run this script it'll check to see if
rem your bot is running and if it's not it'll restart it.
rem (Serves about the same functionality as the botchk
rem script that comes with Eggdrop.)

rem Reasons:
rem Before I made this batch file I always had to manually
rem erase the pid file before Eggdrop would let itself start..
rem And I wanted something that worked similar to the botchk
rem shell script that comes with Eggdrop...so here it is. =)

rem Notes:
rem It's working directory should be the same as your bot.
rem Please keep in mind that this batch doesn't take the place of cron/crontab
rem as in UNIX..  And I recommend against using the Task Scheduler in Windows
rem to run it periodiocally, as "Windrops" are fairly stable anyway and it'd
rem be annoying for you to see a DOS window popping up every few minutes
rem to run this batch.
rem The best way to run this batch is from your Startup folder.
rem Or if you use ICQ, using the Net Detect thingie. =)

rem Requirements:
rem Eggdrop v1.5.4+ - Because it's the only versions that create pid.* files under Windows..
rem Also you need ps.exe from Cygwin - http://SourceWare.Cygnus.com/cygwin/

rem Usage:
rem Put ps.exe and botchk.bat in the same directory as your eggdrop.exe
rem And edit this batch for your bot...

rem Change this to the name of your bot's config:
set botscript=eggdrop.conf

rem Change this to the handle/botnet-nick of your bot:
set botname=Proteus

rem Change this to the name of your bot's userfile:
set userfile=Proteus.user

rem You probably don't need to change anything below here....
if exist .share* del .share*
if exist *.stackdump del *.stackdump
if exist *.tmp del *.tmp
if not exist %botscript% echo Config file (%botscript%) missing.
if not exist %botscript% goto cantload
if exist %userfile% goto checkrunning
if exist %userfile%~new ren %userfile%~new %userfile%
if exist %userfile% echo Missing userfile... Using the last saved one..
if exist %userfile% goto checkrunning
if exist %userfile%~bak ren %userfile%~bak %userfile%
if exist %userfile% echo Missing userfile... Using the last backed up one..
if exist %userfile% goto checkrunning
if not exist %userfile% echo Userfile missing.  No backup/last saved userfile either. =/
goto cantload

:checkrunning
if not exist pid.%botname% goto restart
if [%2]==[] %0 pid.%botname% Result
> {a}.bat echo @prompt set {t}=
> {b}.bat %comspec% /e:1536 /c {a}.bat | find "set"
>>{b}.bat type pid.%botname%
call {b}.bat
for %%v in (%{t}%) do set %2=%%v
set {t}=
del {?}.bat
ps.exe -a | find " %result% " | find /I "/EGGDROP" > nul
if errorlevel 1 goto restart
goto end

:restart
set result=
if exist pid.%botname% echo Found stale pid.%botname%.. Erasing...
if exist pid.%botname% del pid.%botname%
if not exist eggdrop.exe echo Eggdrop.exe not found!
if not exist eggdrop.exe goto cantload
if exist eggdrop.exe echo Restarting Eggdrop...
if exist eggdrop.exe eggdrop.exe %botscript%
if errorlevel 8 if not exist pid.%botname% echo Possibly Missing .dll File(s)?  (tcl8*.dll, cygwin1.dll)
if errorlevel 8 if not exist pid.%botname% goto cantload
if errorlevel 3 if exist pid.%botname% goto success
rem if errorlevel 2 if exist pid.%botname% goto success
echo Eggdrop.exe failed to load. =(
rem if errorlevel 1 goto cantload
rem if errorlevel 0 goto cantload
goto cantload

:cantload
echo Could not reload the bot. =(
if exist pid.%botname% del pid.%botname%
goto end

:success
echo Successfully (Re)Started Eggdrop (%botname% - %botscript%).

:end
